package com.winchain.chainbill.bill.biz.report.service.impl;

import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.dao.CbDctDao;
import com.winchain.chainbill.bill.base.dao.CbRecDao;
import com.winchain.chainbill.bill.biz.discount.vo.DctCalculatedInterestInfoVO;
import com.winchain.chainbill.bill.biz.discount.vo.DctCalculatedInterestQueryVO;
import com.winchain.chainbill.bill.biz.discount.vo.DctCalculatedInterestTotalVO;
import com.winchain.chainbill.bill.biz.discount.vo.DctInterestBusinessDailyEarnTotalVO;
import com.winchain.chainbill.bill.biz.receive.vo.DelayCalculatedInterestInfoVO;
import com.winchain.chainbill.bill.biz.receive.vo.DelayCalculatedInterestQueryVO;
import com.winchain.chainbill.bill.biz.receive.vo.DelayCalculatedInterestTotalVO;
import com.winchain.chainbill.bill.biz.report.service.BillReportQueryService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("billReportQueryService")
public class BillReportQueryServiceImpl implements BillReportQueryService {
	@Autowired
	private CbRecDao cbRecDao;
	@Autowired
	private CbDctDao cbDctDao;

	@Pagination
	public List<DelayCalculatedInterestInfoVO> selectDelayCalculatedInterestByPage(PageLimit limit,
			DelayCalculatedInterestQueryVO delayCalculatedInterestQueryVO) {
		return this.cbRecDao.selectDelayCalculatedInterestList(delayCalculatedInterestQueryVO);
	}

	public DelayCalculatedInterestTotalVO selectDelayCalculatedInterestTotal(
			DelayCalculatedInterestQueryVO delayCalculatedInterestQueryVO) {
		return this.cbRecDao.selectDelayCalculatedInterestTotal(delayCalculatedInterestQueryVO);
	}

	@Pagination
	public List<DctCalculatedInterestInfoVO> selectDctCalculatedInterestByPage(PageLimit limit,
			DctCalculatedInterestQueryVO dctCalculatedInterestQueryVO) {
		return this.cbDctDao.selectDctCalculatedInterestByPage(dctCalculatedInterestQueryVO);
	}

	public DctInterestBusinessDailyEarnTotalVO selectDctInterestBusinessDailyEarnTotal(
			DctCalculatedInterestQueryVO dctCalculatedInterestQueryVO) {
		DctCalculatedInterestTotalVO dctCalculatedInterestTotalVO = this.cbDctDao
				.selectDctCalculatedInterestTotal(dctCalculatedInterestQueryVO);

		DctInterestBusinessDailyEarnTotalVO dctInterestBusinessDailyEarnTotalVO = new DctInterestBusinessDailyEarnTotalVO();
		String currMonth = dctCalculatedInterestQueryVO.getMonth();
		BigDecimal dctAmtTol = dctCalculatedInterestTotalVO.getDctAmtTotal();
		dctInterestBusinessDailyEarnTotalVO.setBusinessDailyTotal(calBusinessDailyTotal(dctAmtTol, currMonth));
		dctInterestBusinessDailyEarnTotalVO.setEarningsTotal(dctCalculatedInterestTotalVO.getCurInsTotal());
		dctInterestBusinessDailyEarnTotalVO
				.setYieldTotal(calYieldTotal(dctInterestBusinessDailyEarnTotalVO.getBusinessDailyTotal(),
						dctInterestBusinessDailyEarnTotalVO.getEarningsTotal(), currMonth));

		dctInterestBusinessDailyEarnTotalVO.setCurInsTotal(dctCalculatedInterestTotalVO.getCurInsTotal());
		dctInterestBusinessDailyEarnTotalVO.setDctIntTotal(dctCalculatedInterestTotalVO.getDctIntTotal());
		dctInterestBusinessDailyEarnTotalVO.setEarlyInsTotal(dctCalculatedInterestTotalVO.getEarlyInsTotal());
		return dctInterestBusinessDailyEarnTotalVO;
	}

	private List<DctInterestBusinessDailyEarnTotalVO> getDctInterestBusinessDailyEarnTotalVO(
			List<DctCalculatedInterestTotalVO> vos) {
		List<DctInterestBusinessDailyEarnTotalVO> rs = new ArrayList();
		for (DctCalculatedInterestTotalVO dctCalculatedInterestTotalVO : vos) {
			DctInterestBusinessDailyEarnTotalVO dctInterestBusinessDailyEarnTotalVO = new DctInterestBusinessDailyEarnTotalVO();
			String currMonth = dctCalculatedInterestTotalVO.getAcrMonth();
			BigDecimal dctAmtTol = dctCalculatedInterestTotalVO.getDctAmtTotal();
			dctInterestBusinessDailyEarnTotalVO.setBusinessDailyTotal(calBusinessDailyTotal(dctAmtTol, currMonth));
			dctInterestBusinessDailyEarnTotalVO.setEarningsTotal(dctCalculatedInterestTotalVO.getCurInsTotal());
			dctInterestBusinessDailyEarnTotalVO
					.setYieldTotal(calYieldTotal(dctInterestBusinessDailyEarnTotalVO.getBusinessDailyTotal(),
							dctInterestBusinessDailyEarnTotalVO.getEarningsTotal(), currMonth));

			dctInterestBusinessDailyEarnTotalVO.setCurInsTotal(dctCalculatedInterestTotalVO.getCurInsTotal());
			dctInterestBusinessDailyEarnTotalVO.setDctIntTotal(dctCalculatedInterestTotalVO.getDctIntTotal());
			dctInterestBusinessDailyEarnTotalVO.setEarlyInsTotal(dctCalculatedInterestTotalVO.getEarlyInsTotal());
			dctInterestBusinessDailyEarnTotalVO.setMonth(currMonth);
			rs.add(dctInterestBusinessDailyEarnTotalVO);
		}
		return rs;
	}

	public List<DctInterestBusinessDailyEarnTotalVO> selectDctInterestBusinessDailyEarnTotalForChart(
			DctCalculatedInterestQueryVO dctCalculatedInterestQueryVO) {
		List<DctCalculatedInterestTotalVO> dctCalculatedInterestTotalVOs = this.cbDctDao
				.selectDctCalculatedInterestTotalForChart(dctCalculatedInterestQueryVO);

		List<DctInterestBusinessDailyEarnTotalVO> rs = null;
		if ((null != dctCalculatedInterestTotalVOs) && (dctCalculatedInterestTotalVOs.size() > 0)) {
			rs = getDctInterestBusinessDailyEarnTotalVO(dctCalculatedInterestTotalVOs);
		}
		return rs;
	}

	private BigDecimal calBusinessDailyTotal(BigDecimal dctAmtTol, String ymonth) {
		int year = Integer.valueOf(ymonth.substring(0, 4)).intValue();
		int month = Integer.valueOf(ymonth.substring(4, ymonth.length())).intValue();
		int days = SysDateUtil.getDaysByYearMonth(year, month);
		return dctAmtTol.divide(new BigDecimal(days), 2, 4);
	}

	private BigDecimal calYieldTotal(BigDecimal businessDailyTotal, BigDecimal earningsTotal, String ymonth) {
		if (businessDailyTotal.compareTo(BigDecimal.ZERO) == 0) {
			return BigDecimal.ZERO;
		}
		int year = Integer.valueOf(ymonth.substring(0, 4)).intValue();
		int month = Integer.valueOf(ymonth.substring(4, ymonth.length())).intValue();
		int days = SysDateUtil.getDaysByYearMonth(year, month);
		return earningsTotal.multiply(BigDecimal.TEN).multiply(BigDecimal.TEN).divide(businessDailyTotal, 5, 4)
				.multiply(new BigDecimal(360)).divide(new BigDecimal(days), 2, 4);
	}
}
